package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
	"k8soperation/pkg/valid"
)

type CommonIdRequest struct {
	ID uint32 `json:"id" form:"id" valid:"id"`
}

func NewCommonIdRequest() *CommonIdRequest {
	return &CommonIdRequest{}
}

func ValidCommonIdRequest(data interface{}, ctx *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"id": []string{"required", "numeric", "min:1"},
	}
	messages := govalidator.MapData{
		"id": []string{
			"required:id不能为空",
			"numeric:id必须是数字",
			"min:id必须大于0",
		},
	}

	// 校验入参
	errs := valid.ValidateOptions(data, rules, messages)

	return errs
}

// KubeCommonRequest 公共请求
type KubeCommonRequest struct {
	Name      string `json:"name" form:"name" valid:"name"`
	Namespace string `json:"namespace" form:"namespace" valid:"namespace"`
}

func NewKubeCommonRequest() *KubeCommonRequest {
	return &KubeCommonRequest{}
}

func VaildKubeCommonRequest(data interface{}, ctx *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"name":      []string{"required"},
		"namespace": []string{"required"},
	}
	messages := govalidator.MapData{
		"name": []string{
			"required: name不能为空",
		},
		"namespace": []string{
			"required: namespace不能为空",
		},
	}

	// 校验入参
	return valid.ValidateOptions(data, rules, messages)
}

// 通用标签
type Label struct {
	Key   string `json:"key"   valid:"key"`
	Value string `json:"value" valid:"value"`
}

// ValidKubeDeploymentCreateRequest 校验创建 Deployment 请求
func ValidKubeDeploymentCreateRequest(data interface{}, ctx *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"name":            []string{"required"},
		"namespace":       []string{"required"},
		"container_image": []string{"required"},
		"replicas":        []string{"required"},
	}
	messages := govalidator.MapData{
		"namespace": []string{"required: namespace不能为空"},
		"name":      []string{"required: name不能为空"},
		"container_image": []string{
			"required: image不能为空",
		},
		"replicas": []string{"required: replicas不能为空"},
	}
	return valid.ValidateOptions(data, rules, messages)
}

// 容器端口映射
type PortMapping struct {
	Port       int32  `json:"port"        valid:"port"`
	TargetPort int32  `json:"target_port" valid:"target_port"`
	Protocol   string `json:"protocol"    valid:"protocol"` // TCP/UDP/SCTP
}

// HTTP 头（用于探针）
type HttpHeader struct {
	Name  string `json:"name"  valid:"name"`
	Value string `json:"value" valid:"value"`
}

// 健康检查探针（readiness/liveness 共用）
type HealthCheckDetail struct {
	Type                string       `json:"type"                  valid:"type"` // HTTP|TCP|Command
	Protocol            string       `json:"protocol"              valid:"protocol"`
	Path                string       `json:"path"                  valid:"path"`
	Port                int32        `json:"port"                  valid:"port"`
	HttpHeader          []HttpHeader `json:"http_header"           valid:"http_header"`
	InitialDelaySeconds int32        `json:"initial_delay_seconds" valid:"initial_delay_seconds"`
	PeriodSeconds       int32        `json:"period_seconds"        valid:"period_seconds"`
	TimeoutSeconds      int32        `json:"timeout_seconds"       valid:"timeout_seconds"`
	SuccessThreshold    int32        `json:"success_threshold"     valid:"success_threshold"`
	FailureThreshold    int32        `json:"failure_threshold"     valid:"failure_threshold"`
	Command             string       `json:"command"               valid:"command"` // Type=Command 时使用
}

/* ========== 通用分页 DTO ========== */

type PageLimit struct {
	Page  int `json:"page"  form:"page"  valid:"page"`  // 页码，从 1 开始
	Limit int `json:"limit" form:"limit" valid:"limit"` // 每页条数
}

/* ========== 通用删除选项 DTO ========== */

type DeleteOptions struct {
	GracePeriodSeconds *int64 `json:"grace_period_seconds,omitempty" valid:"grace_period_seconds"`
	Force              bool   `json:"force,omitempty"                 valid:"force"`
	Wait               bool   `json:"wait,omitempty"                  valid:"wait"`
	TimeoutSeconds     *int64 `json:"timeout_seconds,omitempty"       valid:"timeout_seconds"`
}

/* ========== 通用事件查询 DTO ========== */

type KubeEventListRequest struct {
	Namespace     string `json:"namespace,omitempty" valid:"namespace"` // 为空=全局
	Kind          string `json:"kind,omitempty"      valid:"kind"`      // Deployment/StatefulSet/DaemonSet/Pod/Node...
	Name          string `json:"name,omitempty"      valid:"name"`
	Type          string `json:"type,omitempty"      valid:"type"` // Normal | Warning
	Reason        string `json:"reason,omitempty"    valid:"reason"`
	Limit         int64  `json:"limit,omitempty"     valid:"limit"` // 默认 50
	ContinueToken string `json:"continue,omitempty"  valid:"continue"`
	SinceSeconds  int64  `json:"since_seconds,omitempty" valid:"since_seconds"`
}

func NewKubeEventListRequest() *KubeEventListRequest {
	return &KubeEventListRequest{
		Limit:        50,
		SinceSeconds: 1000,
	}
}

/* ========== 通用校验辅助（可选） ========== */

// 详情/删除/重启等：Name & Namespace 必填
func ValidNameNamespace(data interface{}, ctx *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"namespace": []string{"required"},
		"name":      []string{"required"},
	}
	msg := govalidator.MapData{
		"namespace": []string{"required: namespace 不能为空"},
		"name":      []string{"required: name 不能为空"},
	}
	return valid.ValidateOptions(data, rules, msg)
}

// 分页规则：Page>=1, 1<=Limit<=200
func ValidPageLimit(data interface{}, ctx *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"page":  []string{"required", "min:1"},
		"limit": []string{"required", "min:1", "max:200"},
	}
	msg := govalidator.MapData{
		"page":  []string{"required: page 不能为空", "min: page 必须 >= 1"},
		"limit": []string{"required: limit 不能为空", "min: limit 必须 >= 1", "max: limit 不能超过 200"},
	}
	return valid.ValidateOptions(data, rules, msg)
}

func ValidKubeEventListRequest(data interface{}, ctx *gin.Context) map[string][]string {
	// namespace/name 可空，这里不加 required，保持通用
	return valid.ValidateOptions(data, govalidator.MapData{}, nil)
}
